{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6224057a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# python 中的一切皆是对象 (数字、字符串、函数、模块)\n",
    "# 列表的使用方法    （列表的方法是会改变列表的）\n",
    "# 列表可能包含不同类型的对象，但一般这些对象都有相同的类型（也就是说一般情况下一个列表内包含的是同一种类型的对象）\n",
    "# 列表是可以有重复值存在\n",
    "\n",
    "# 1.列表——增加\n",
    "#  append()  增加元素到list的末位 按增加的顺序依次在末位排列\n",
    "#            一般用来新建列表会使用到\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "447211ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'kiki']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'kiki', 'Coco']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.append(\"kiki\")\n",
    "print(names)   #  ['Mike', 'Mary', 'Jan', 'Jack', 'kiki']\n",
    "names.append(\"Coco\")\n",
    "print(names)    # ['Mike', 'Mary', 'Jan', 'Jack', 'kiki', 'Coco']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6bac82cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Jan', 'Jack']\n",
      "['Mike', 'Mary', 'Lihua', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "#案例：\n",
    "names_J = []  # 建一个空列表\n",
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "for i in names:  # 利用for循环把列表的每个元素取出 赋给新变量i\n",
    "    if i[0] == \"J\":   #判断 ：是否变量i被赋予的对象（此时对象为字符串 是一个有序的序列）的索引位置为0的那个元素=”J“\n",
    "        names_J.append(i)    # 是的话 就把i所对应的那个对象添加到空列表中\n",
    "print(names_J)   # ['Jan', 'Jack']\n",
    "\n",
    "# 2. 列表——插入元素\n",
    "# list.insert(要插入的位置的索引值,要插入的元素)\n",
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.insert(2,\"Lihua\")\n",
    "print(names)   # ['Mike', 'Mary', 'Lihua', 'Jan', 'Jack']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d807ecc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'ZZ', 'Jack', 'ZZ', 'kiki']\n",
      "['Mike', 'Mary', 'Jack', 'ZZ', 'kiki']\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 3.列表——删除/移除\n",
    "# list.remove(要删除的对象)  : 使用之前，应该先判断该元素是否在list中用（in查询）\n",
    "#             remove删去的是列表中重复值的第一个（一次只能删一个）\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "print(names)\n",
    "names.remove(\"ZZ\")\n",
    "print(names)  # ['Mike', 'Mary', 'Jack', 'ZZ', 'kiki']\n",
    "# in查询拓展\n",
    "names = ['Mike', 'Mary', 'Lihua', 'Jan', 'Jack']\n",
    "print(\"Lihua\" in names)  # 查询Lihua是否在列表中，在就会打印出True ，否则打印False\n",
    "# 案例(删去所有的重复值用remove方法)\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "names_user = input(\"删除内容：\")\n",
    "while True:\n",
    "    if names_user in names:\n",
    "        names.remove(names_user)\n",
    "    else:\n",
    "        break\n",
    "print(\"删除后的列表：\",names)\n",
    "\n",
    "# 4.列表——删除\n",
    "#   list.pop(要删去对象的索引值)    : list.pop()括号内不填内容的话，默认会删去列表的最后一个对象  --> 对应记忆：append() 默认在列表末位添加对象\n",
    "#   pop()同样也是一次删去一个对象\n",
    "#   利用pop删去重复值就要用到枚举  -->《知识点_列表的高级用法之枚举》\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "names.pop()  # 删去最后一个\n",
    "print(names)  # ['Mike', 'Mary', 'ZZ', 'Jack', 'ZZ']\n",
    "names.pop(3)   # 删去Jack\n",
    "print(names)   # ['Mike', 'Mary', 'ZZ', 'ZZ']\n",
    "\n",
    "# 和 pop()功能相似的另外一个方法 del\n",
    "# del指定列表中的某个对象的索引值进行删除\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "del names[1] # 写法： del list[要删除对象的索引值！！！此处一定不能为空]\n",
    "print(names)  # ['Mike', 'ZZ', 'Jack', 'ZZ', 'kiki']\n",
    "\n",
    "# index 索引的拓展\n",
    "# 写法：str.index(str, 开始索引（默认从0），结束索引（默认为字符串的长度）)  --> (str,检索范围 不写就默认为全部)\n",
    "# 字符串中的index：\n",
    "favor = \"i like cat\"  # 注意空格也属于字符串\n",
    "print(favor.index(\"e\"))  # 5\n",
    "# 列表中的index：\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "print(names.index(\"ZZ\")) # 2(这个是列表中第一个ZZ的索引位置)\n",
    "\n",
    "# 5.列表——排序\n",
    "# list.sort()   :默认从小到大排序\n",
    "# reverse = Fales(默认)  小-->大\n",
    "# reverse = True   大-->小\n",
    "stu_score = [90,89,95,100,96,93,82]\n",
    "stu_score.sort()\n",
    "print(stu_score)  # [82, 89, 90, 93, 95, 96, 100]\n",
    "stu_score.sort(reverse = True)\n",
    "print(stu_score)  # [100, 96, 95, 93, 90, 89, 82]\n",
    "# 当列表为字符串文本时  排序方式以每个字符串的首字母按ASCII编码表的顺序\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "names.sort()\n",
    "print(names)  # ['Jack', 'Mary', 'Mike', 'ZZ', 'ZZ', 'kiki']\n",
    "\n",
    "# 6.列表——查看对象的次数\n",
    "# list.count()\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "print(names.count(\"ZZ\"))\n",
    "stu_score = [90,89,90,100,96,93,82,90,91,98,99,90]\n",
    "print(stu_score.count(90))\n",
    "\n",
    "# 7.列表——清空\n",
    "# list.clear()\n",
    "names = [\"Mike\",\"Mary\",\"ZZ\",\"Jack\",\"ZZ\",\"kiki\"]\n",
    "names.clear()\n",
    "print(names)  # []\n",
    "\n",
    "# 8.列表——合并\n",
    "# list.extend()   : 列表合并是有顺序的，先操作哪个列表 那个列表就先在前面\n",
    "names1 = [\"A\",\"B\",\"C\"]\n",
    "names2 = [\"D\",\"E\"]\n",
    "names1.extend(names2)  # 这里是在names1中调用names2 相当于先操作的是names1 (list2.extend(list1)  这样就说明列表2一定在列表1前面)\n",
    "print(names1)  # ['A', 'B', 'C', 'D', 'E']\n",
    "\n",
    "nums = [5,4]\n",
    "nums.extend([3,2,1])\n",
    "print(nums)   # [5, 4, 3, 2, 1]\n",
    "\n",
    "# 9.列表——修改\n",
    "# 直接找到列表中某个对象 确定其索引值，然后用赋值的方式把新对象赋给列表要替换的那个值\n",
    "names3 = [\"A\",\"B\",\"C\",\"D\",\"E\"]\n",
    "names3[2] = \"G\"\n",
    "print(names3)  # ['A', 'B', 'G', 'D', 'E']\n",
    "\n",
    "# 课本对应项目解析：将“Don't panic”-->\"on tap\"\n",
    "phrase = \"Don't panic!\"  # 将字符串赋给变量\n",
    "plist = list(phrase)     #  str 转换 list 使用的方法是 list(str)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):    # for循环执行4次 删除末位 使用的方法 pop()\n",
    "    plist.pop()\n",
    "print(plist)    # 删去后的结果['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
    "plist.pop(0)\n",
    "print(plist)  # ['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
    "plist.remove(\"'\")\n",
    "print(plist)  # ['o', 'n', 't', ' ', 'p', 'a']\n",
    "# extend ==>plist + [plist.pop(),plist.pop()]\n",
    "plist.extend([plist.pop(),plist.pop()])  # ptyhon运算是有顺序的，先算小括号再运算大括号\n",
    "# ['o', 'n', 't', ' '] a        p      # 理解为 先将末位的a和p依次用pop取出 取出时将变为对象了，这时再将这两个字母变为列表加上[],最后再合并到plist中去\n",
    "print(plist)  # ['o', 'n', 't', ' ', 'a', 'p']\n",
    "plist.insert(2,plist.pop(3))        # 用插入的方法把删除了的空格再插入进列表指定位置\n",
    "print(plist)  # ['o', 'n', ' ', 't', 'a', 'p']\n",
    "new_phrase = ''.join(plist)     # 最后只要把列表变为字符串 用join方法 ： ''.join(list) 引号里的是指列表对象转换为字符串后用什么来连接的条件\n",
    "print(\"这是新列表=\",plist)  # 用列表的方法会改变列表的内容\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128ba617",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
